package com.xbongbong.paas.service.impl;

import com.xbongbong.paas.redis.PaasRedisHelper;
import com.xbongbong.paas.redis.RedisTimeConstant;
import com.xbongbong.paas.service.I18nService;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.i18n.pojo.dto.SetLangPackDTO;
import com.xbongbong.pro.i18n.pojo.vo.SetLangPackVO;
import com.xbongbong.sys.enums.UserConfigEnum;
import com.xbongbong.sys.model.CompanyConfigModel;
import com.xbongbong.sys.model.UserConfigModel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import static com.xbongbong.paas.redis.RedisPrefixConstant.PC_I18N_PACK;

/**
 * 国际化相关逻辑实现
 *
 * @author kaka
 * @version v1.0
 * @date 2020/4/21 11:16
 * @since v1.0
 */
@Service("i18nService")
public class I18nServiceImpl implements I18nService {

    private static final Logger LOG = LoggerFactory.getLogger(GuideLableServiceImpl.class);

    @Resource
    private PaasRedisHelper paasRedisHelper;
    @Resource
    private CompanyConfigModel companyConfigModel;
    @Resource
    private UserConfigModel userConfigModel;

    @Override
    public SetLangPackVO setLangPack(SetLangPackDTO setLangPackDTO) throws XbbException {
        SetLangPackVO setLangPackVO = new SetLangPackVO();
        try {
            String corpid = setLangPackDTO.getCorpid();
            String userId = setLangPackDTO.getUserId();
            String lang = setLangPackDTO.getLang();

            String key = corpid + "_" + userId;
            paasRedisHelper.setValue(PC_I18N_PACK, key, lang, RedisTimeConstant.LONG_DURATION);

            userConfigModel.save(corpid, userId, UserConfigEnum.I18N_PACK.getAlias(), UserConfigEnum.I18N_PACK.getName(), lang);
//            companyConfigModel.save(corpid, CompanyConfigEnum.PC_I18N_PACK.getAlias(), CompanyConfigEnum.PC_I18N_PACK.getName(), lang);
            setLangPackVO.setCorpid(corpid);
            setLangPackVO.setLang(lang);
        } catch (Exception e) {
            LOG.error("setLangPack error", e);
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, SystemErrorCodeEnum.API_ERROR_100001.getMsg());
        }
        return setLangPackVO;
    }
}
